const db = wx.cloud.database()
Page({
  data: {
    list: [],
    show: false,
    allDisplay: "none",
  },
  ok(e) {
    var that = this;
    wx.showLoading({
      title: '通过中...',
    })
    //通过
    wx.cloud.callFunction({
      name: 'update_show',
      data: {
        id:e.currentTarget.dataset.id
      }
    }).then(res => {
      that.getList()
    })
  },
  onLoad: function (e) {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    that.getCount()
    that.getList()
  },
  //获取条数
  getCount() {
    var that = this;
    db.collection("say").where({
      show: false
    }).count({
      success: function (res) {
        console.log(res.total)
        that.setData({
          dataSum: res.total
        })
      }
    })
  },
  //获取6条数据
  getList() {
    var that = this
    db.collection('say').limit(6).orderBy('time', 'desc').where({
      show: false
    }).get({
      success: function (res) {
        that.setData({
          list: res.data,
          allDisplay: ""
        })
      },
      fail: function (event) {}
    })
    wx.hideLoading({
      success: (res) => {},
    })
  },

  /* 
   * 页面上拉触底事件的处理函数图片
   * 
   */
  onReachBottom: function () {
    var that = this;
    var temp = [];
    if (that.data.list.length < that.data.dataSum) {
      try {
        wx.showLoading({
          title: '加载中...',
        })
        db.collection('say').skip(that.data.list.length).limit(6).orderBy('time', 'desc').where({
          show: false
        }).get({

          success: function (res) {

            if (res.data.length > 0) {
              for (var i = 0; i < res.data.length; i++) {
                var tempshopping = res.data[i];
                temp.push(tempshopping);
              }
              var dataList = {};
              dataList = that.data.list.concat(temp);
              that.setData({
                list: dataList,
              })
            }
            wx.hideLoading();
          },
          fail: function (event) {
            console.log("======" + event);
          }
        })
      } catch (e) {
        console.error(e);
      }
    } else if (that.data.list.length >= that.data.dataSum) {
      wx.showToast({
        title: '已为你加载全部',
        icon: 'none'
      })
    }
  },
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      allDisplay: "none"
    })
    that.getList()
    setTimeout(function () {
      wx.stopPullDownRefresh() //停止下拉刷新

      that.setData({
        allDisplay: ""
      })
    }, 1500);
  },
  lookImage(e) {
    console.log(e)
    var that = this;
    var src = e.target.dataset.url; //获取data-src
    var imgList = [e.target.dataset.url]; //获取data-list
    //图片预览
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: imgList // 需要预览的图片http链接列表
    })
  }
})